<template>
  <div ref="rating" class="ratingList">
    <div>
      <div class="rating display-flex ai-center">
        <div class="text-center allRating p-3">
          <p class="orange-1 font-number-1">{{info.score}}</p>
          <p>综合评分</p>
          <p class="grey-4 font-xss">高于周边商家99%</p>
        </div>
        <div class="flex-1">
          <div class="pl-6">
            <Star :score="info.serviceScore" :size="36"/>
            <div>
              <span class="pl-3">服务态度</span>
              <span class="orange-1 font-s font-weight">{{info.serviceScore}}</span>
            </div>
          </div>
          <div class="pl-6">
            <Star :score="info.foodScore" :size="36"/>
            <div class="pl-2 ">
              <span class="pl-3">商品评分</span>
              <span class="orange-1 font-s font-weight">{{info.foodScore}}</span>
            </div>
          </div>
          <div class="pl-4 pt-1">
            <span class="font-s">送达时间</span>
            <span class="pl-2 grey-4 font-xs">28分钟</span>
          </div>
        </div>
      </div>
      <div class="mt-3 bg-white content p-4" v-if="ratings">
        <div class="display-flex">
          <div class="sign mr-3" @click="selectType = 2" :class="{signClick:selectType === 2}">
            全部<span class="font-small pl-1">{{info.ratingCount}}</span>
          </div>
          <div class="sign mr-3" @click="selectType = 0" :class="{signClick:selectType === 0}">
            满意<span class="font-small pl-1">{{positiveSize}}</span>
          </div>
          <div class="sign mr-3" @click="selectType = 1" :class="{signClick:selectType === 1}">
            不满意<span class="font-small pl-1">{{info.ratingCount - positiveSize}}</span>
          </div>
        </div>
        <div class="checkIcon display-flex ai-center mt-4 pb-2 border-b" @click="onlyShowText = !onlyShowText" :class="{active:onlyShowText}">
          <i class="iconfont icon-check-circle" ></i>
          <span class="font-s grey-4 pl-1">只看有内容的评论</span>
        </div>
        <ul v-if="filterRatings">
          <li v-for="(item,index) in filterRatings" :key="index">
            <div class="display-flex mt-3 pb-3 border-b">
              <img :src="item.avatar" alt="" width="28px" height="28px" style="border-radius: 50%">
              <div class="pl-2" style="width: 100%;">
                <div class="display-flex jc-between">
                  <div class="font-xss" style="color: #07111b">{{item.username}}</div>
                  <div class="font-xss">{{item.rateTime | dateFormat}}</div>
                </div>
                <div class="display-flex ai-center">
                  <Star :score="item.score" :size="24"/>
                  <span class="font-xss grey-4 pl-1">{{item.deliveryTime}}</span>
                </div>
                <div class="font-s pt-2">{{item.text}}</div>
                <div class="display-flex ai-center wrap">
                  <i v-if="!item.rateType" class="iconfont icon-icon-thumb-up-solid pr-2" style="font-size:9px;color: #f5a100"></i>
                  <i v-else class="iconfont icon-icon-thumb-down pr-2 grey-4" style="font-size:9px"></i>
                  <ul v-if="item.recommend" class="display-flex">
                    <li v-for="(commend,index) in item.recommend" :key="index" >
                      <span class="grey-4 recommend">
                        {{commend}}
                      </span>
                    </li>
                  </ul>
                </div>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
  import Star from "../../../components/Star/Star";
  import {mapState,mapGetters} from "vuex";
  import BScroll from 'better-scroll'
  import format from 'date-fns/format'
  // import moment from 'moment'
  export default {
    name: "ShopRatings",
    computed: {
      ...mapState({info: 'info', ratings: 'ratings'}),
      ...mapGetters({positiveSize:'positiveSize'}),
      filterRatings() {
        const {ratings, onlyShowText, selectType} = this
        //产生一个过滤新数组
        /*
            评论列表显示必须具备两个条件：
                选择评价类型------全部？满意？不满意
                是否只显示有文本内容
            当点击全部时，需判断是否显示有文本内容
                            (显示的话：onlyShowText为true，text不能为空)
                            (不显示的话：onlyShowText为false，不需要判断text是否为空)
            当点击满意或不满意时，需判断是否显示有文本内容
                            rateType有两种数据，当 rateType = 0 时，为满意的数据， 当 rateType = 1 时，为不满意的数据
                            (显示的话,rateType必须等于selectType，text不能为空)
                            (不显示的话：onlyShowText为false，不需要判断text是否为空)
             */
        if (Array.isArray(ratings)) {
          return ratings.filter(rating => {
            const {rateType, text} = rating
            return (selectType === 2 || selectType === rateType) && (!onlyShowText || text.length > 0)
          })
        } else {
          return {}
        }

      },
    },
    components:{
      Star
    },
    filters:{
      dateFormat(dateStr,partten="yyyy-MM-dd HH:mm:ss"){
        // return moment(dateStr).format(partten)
        return format(dateStr,partten)
      }
    },
    data(){
      return {
        onlyShowText:true,  //是否只显示有文本内容
        selectType:2 //选择的评价类型：0--满意、1--不满意、2--全部
      }
    },
    created() {
      this.$store.dispatch('getShopRatings',()=>{
        this.$nextTick(()=>{
          new BScroll(this.$refs.rating,{
            click:true
          })
        })
      })
    }
  }
</script>

<style scoped lang="scss">
  .ratingList{
    height: calc(100vh - 150px - 38px);
    overflow: hidden;
    .rating{
      background-color: #fff;
      .allRating{
        position: relative;
        &::after{
          content: '';
          height: 70%;
          width: 1px;
          background-color: rgba(7,17,27,0.1);
          position: absolute;
          right: 0;
          top: 16px;
        }
      }
    }
    .content{
      .sign {
        background-color: rgba(77,85,93,0.2);
        color: #4d555d;
        border-radius: 1px;
        padding: 8px 12px;
        &.signClick{
          background-color: #02a774;
          color: #fff;
        }
      }
      .checkIcon{
        color: #93999f;
        &.active{
          color: #02a774;
        }
        .iconfont{
          font-size: 24px;
        }
      }
      .recommend{
        font-size: 9px;
        padding: 0 6px;
        background: #fff;
        border: 1px solid rgba(7,17,27,0.1);
      }
    }
  }

</style>
